﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using StudentMgmt.Model;
using StudentMgmt.SQLServerDAL;
using System.Configuration;
using System.Xml.Linq;

namespace StudentMgmt
{
    public partial class frmStuInfoChange : Form
    {
        public frmStuInfoChange()
        {
            InitializeComponent();
        }
        private void frmNewChange_Load(object sender, EventArgs e)
        {

            string cityPath = ConfigurationManager.AppSettings["CityXMLPath"].ToString();
            cityPath = cityPath + "citys.xml";
            XDocument doc;
            try
            {
                doc = XDocument.Load(cityPath);
            }
            catch (Exception)
            {
                     throw;
            }
            List<string> citys = new List<string>();
            var query = from t in doc.Descendants("province")
                        select new { 
                            city = t.Attribute("name").Value
                        };
            if (query.Count()>0){
                foreach (var item  in query)
	            {
		             citys.Add(item.city.ToString() );
	            }
            } 
            this.cmbProvince.ValueMember = "city";
            this.cmbProvince.DisplayMember = "city";
            this.cmbProvince.DataSource = citys;//此处若先绑定Datasource会提示无法绑定
	 
        }
        private void btuSearch_Click(object sender, EventArgs e)
        {
            //查询学生信息
            string stuNo = this.tbStuNo.Text.Trim();
            Student student = new Student();
            DataTable myTable = new DataTable();
            int val = 0;
            if( stuNo!="" ){
                 val = student.QueryStudent(stuNo, "" , "" , "" , out myTable);
            }
            else
            {
                MessageBox.Show("未输入学号！");
                return;
            }

            //显示学生信息
            if( val==1 && myTable.Rows.Count>0 ){ 
                tbStuName.Text = myTable.Rows[0]["学生姓名"].ToString();

                if (myTable.Rows[0]["性别"].ToString()=="男")
                {
                    this.radMale.Checked = true;
                }
                else {
                    this.radFemale.Checked = true;           
                }

                DateTime datetime = DateTime.Parse(myTable.Rows[0]["出生日期"].ToString());//将字符串转化为datetime格式，还可以用string.Format
                dtBirthday.Value = datetime;
               // MessageBox.Show(datetime.ToString());
                for (int i = 0; i < cmbProvince.Items.Count; i++)
                {
                    if (myTable.Rows[0]["籍贯"].ToString() == cmbProvince.Items[i].ToString())
                    {
                        this.cmbProvince.SelectedIndex = i ;
                        break;
                    }
                }
                this.labelClass.Text = myTable.Rows[0]["所在班级"].ToString();
             
            }
            else
            {
                MessageBox.Show("该学号不存在！");
                return;
            }
        }
        private void btuCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

      

        private void btuSubmit_Click(object sender, EventArgs e)
        {
            Student student = new Student();
            StudentInfo studentInfo = new StudentInfo();
            studentInfo.StudentNo = this.tbStuNo.Text.Trim().ToUpper();
            studentInfo.StudentName = this.tbStuName.Text.Trim();
             if (radFemale.Checked){
                 studentInfo.Sex ="F";
            }
             else
             {
                 studentInfo.Sex = "M";
             }
             studentInfo.NativePlace = this.cmbProvince.SelectedValue.ToString();
             studentInfo.ClassId = this.labelClass.Text;
             string strTime = (this.dtBirthday.Value).ToString().Substring(0,9);
             studentInfo.Birthday = strTime.ToString() ;
             student.AlterStudent(studentInfo);
             MessageBox.Show("修改成功！");
         
        }

      
    }
}
